package org.csu.jpetstore.web.servlet;

import org.csu.jpetstore.domain.Account;
import org.csu.jpetstore.domain.Cart;
import org.csu.jpetstore.domain.Order;
import org.csu.jpetstore.service.CartService;
import org.csu.jpetstore.service.LogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;

public class ViewFinalOrderServlet extends HttpServlet {

    private CartService cartService;
    private LogService logService;

    private static final String VIEW_FINAL_ORDER = "/WEB-INF/jsp/order/FinalOrder.jsp";

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        cartService = new CartService();
        logService = new LogService();

        HttpSession session = request.getSession();
        Order order = (Order)session.getAttribute("order");
        int id = order.getOrderId();

        Account user = (Account) session.getAttribute("loginAccount");
        String username = user.getUsername();
        if(user != null) {
            Date date = new Date();
            logService.insert(username, date.toString(), "提交订单");
        }

        request.getRequestDispatcher(VIEW_FINAL_ORDER).forward(request, response);
        session.setAttribute("cart",null);
        cartService.subimt(username, id);
    }
}
